//
//  QDLoginController.m
//  QuickDoctor
//
//  Created by ycw on 2016/12/20.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "QDLoginController.h"
#import "QDMainViewController.h"
#import "QDSideslippingViewController.h"
#import "NetworkToolOther.h"

@interface QDLoginController ()

@property(weak,nonatomic) UITextField *txfUsername;

@property(weak,nonatomic) UITextField *txfPassword;

//侧滑控制器
@property (nonatomic, strong) JASidePanelController *panelController;

@end

@implementation QDLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:23/255.0 green:187/255.0 blue:187/255.0 alpha:1];
    
    self.title = @"登录";
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"JianTou"] style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftButton)];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
    [self setUpUI];
    
}

#pragma mark
#pragma mark - 设计界面
- (void)setUpUI
{
    
    UIImageView *imgV = [[UIImageView alloc] init];
    
    [self.view addSubview:imgV];
    
    imgV.image = [UIImage imageNamed:@"product_img_logo_nor.png"];
    
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.view).mas_offset(-120);
        
        make.centerX.equalTo(self.view);
        
        make.height.mas_equalTo(100);
        
        make.width.mas_equalTo(70);
        
    }];
    
    UIImageView *imgTitle = [[UIImageView alloc] init];
    
    [self.view addSubview:imgTitle];
    
    imgTitle.image = [UIImage imageNamed:@"product_text_logo_nor.png"];
    
    [imgTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(imgV.mas_bottom);
        
        make.height.mas_equalTo(50);
        
        make.width.mas_equalTo(70);
        
        make.leading.equalTo(imgV);
        
    }];
    
    UITextField *txfUsername = [[UITextField alloc] init];
    
    txfUsername.placeholder = @"请输入账号";
    
    txfUsername.borderStyle = UITextBorderStyleRoundedRect;
    
    //关闭大小写,联想
    [txfUsername setAutocorrectionType:UITextAutocorrectionTypeNo];
    [txfUsername setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    
    [self.view addSubview:txfUsername];
    
    [txfUsername mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(imgTitle.mas_bottom).mas_offset(20);
        
        make.leading.equalTo(self.view).mas_offset(20);
        
        make.trailing.equalTo(self.view).mas_offset(-20);
        
    }];
    
    self.txfUsername = txfUsername;
    
    UITextField *txfPassword = [[UITextField alloc] init];
    
    txfPassword.placeholder = @"请输入密码";
    
    txfPassword.borderStyle = UITextBorderStyleRoundedRect;
    txfPassword.secureTextEntry = YES;
    
    //关闭大小写,联想
    [txfPassword setAutocorrectionType:UITextAutocorrectionTypeNo];
    [txfPassword setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    
    
    [self.view addSubview:txfPassword];
    
    [txfPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(txfUsername.mas_bottom).mas_offset(20);
        
        make.leading.equalTo(self.view).mas_offset(20);
        
        make.trailing.equalTo(self.view).mas_offset(-20);
        
    }];
    
    self.txfPassword = txfPassword;
    
    UIButton *loginButton = [[UIButton alloc] init];
    
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    
    loginButton.backgroundColor = [UIColor colorWithRed:23/255.0 green:187/255.0 blue:187/255.0 alpha:1];
    
    [self.view addSubview:loginButton];
    
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(txfPassword.mas_bottom).mas_offset(30);
        
        make.leading.equalTo(txfPassword);
        
        make.trailing.equalTo(txfPassword);
        
        make.height.mas_equalTo(40);
        
    }];
    
    [loginButton addTarget:self action:@selector(clickLoginButton) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark
#pragma mark - 让键盘弹下去
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //
    //    [SVProgressHUD showSuccessWithStatus:@"登录成功"];
    
    //[SVProgressHUD show];
    //
    //[SVProgressHUD setSuccessImage:[UIImage imageNamed:@"success"]];
    
    
    
    //[SVProgressHUD dismissWithDelay:2];
    
    //    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //        // time-consuming task
    //        dispatch_async(dispatch_get_main_queue(), ^{
    //            [SVProgressHUD dismiss];
    //        });
    //    });
    //
    [self.view endEditing:YES];
    
}

#pragma mark
#pragma mark - 点击跳回
- (void)clickLeftButton
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark
#pragma mark - 实现点击登录按钮
- (void)clickLoginButton
{
    
    
    
    NSString *username = self.txfUsername.text;
    NSString *password = self.txfPassword.text;
    
    NSString *urlString = @"http://127.0.0.1/login.php";
    NSDictionary *parmas = @{@"username":username,@"password":password};
    
    [[NetworkToolOther shareManager] POST_RequestWithUrlString:urlString parmas:parmas successBloock:^(NSDictionary *responseObject) {
        NSLog(@"登录成功");
        
        self.panelController = [[JASidePanelController alloc] init];
        
        QDSideslippingViewController *sidesVC = [[QDSideslippingViewController alloc] init];
        
        self.panelController.leftPanel = sidesVC;
        self.panelController.leftPanel.view.backgroundColor = [UIColor whiteColor];
        
        QDMainViewController *viewC = [QDMainViewController new];
        self.panelController.centerPanel = [[UINavigationController alloc] initWithRootViewController:viewC];
        
        sidesVC.tableView.tableHeaderView = [sidesVC createHeaderView];
        
        
        [self presentViewController:self.panelController animated:NO completion:nil];
        
        [SVProgressHUD showSuccessWithStatus:@"登录成功"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        
        
        //记录登录状态
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:KEYFORLOGIN];
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"登录失败 ----> %@",error);
        
        [SVProgressHUD showErrorWithStatus:@"登录失败"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        
    }];
    
    
    
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    
    
    
}






@end
